package com.cssw.fyzb.core.dao.system;


import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Auther: zzf
 * @Date: 2020/12/2 15:31
 * @Description:
 */
@Repository
public class SysUserDaoImpl implements SysUserDao {

    @Override
    public Page<User> list(int page, int limit, Map param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select t1.*,t2.user_name as sup_user_name");
        sqlParse.addSQL(" from t_user t1 left join  t_user t2 on t1.sup_user_id=t2.user_id where 1=1  ");
        sqlParse.addSQL(" and t1.user_id = :userId ");
        sqlParse.addLikeSQL(" and t1.user_name like :userName ");
        sqlParse.addLikeSQL(" and t1.nick_name like :nickName ");
        sqlParse.addLikeSQL(" and t2.user_name like :supUserName ");
        sqlParse.addLikeSQL(" and t2.sup_user_id = :supUserId ");
        sqlParse.addLikeSQL(" and t1.mobile like :mobile ");
        sqlParse.addLikeSQL(" order by t1.create_time desc");
        return User.dao.paginate(page, limit, sqlParse);
    }

    @Override
    public User getDetails(String userId) {
        StringBuilder str = new StringBuilder();
        str.append("select t1.*,(select t2.source_name from t_user_source where t2.user_id=t1.user_id )source_name, ")
                .append("  t2.wechat_no,t2.create_time wechatCreatetTime , ")
                .append(" (select t2.user_name from t_user where t2.user_id=t1.user_id  )sup_user_name ")
                .append(" t2.update_time as wechat_update_time, t2.ali_no ,t3.create_time ali_createt_time,t3.update_time ali_update_time")
                .append(" , (select t2.level_name from d_vip_level t2 where t1.user_level=t2.level_id)vip_level_name")
                /*.append(" ,(select t2.level_name from d_team_level t2 where t1.team_level=t2.level_id)")*/
                .append(",t4.* from t_user t1 ")
                .append(" left join t_user_wechat t2 on t1.user_id=t2.user_id ")
                .append(" left join t_user_ali t3 on t1.user_id=t3.user_id  ")
                .append(" left join t_user_ext t4 on t1.user_id=t4.user_id ")
                .append(" where t1.user_id=:? limit 1 ");
        return User.dao.findFirst(str.toString(), userId);
    }

    /**
     * 更新商品状态
     *
     * @param userId
     * @param status
     */
    public void updateStatus(Long userId, String status) {
        User offer = new User();
        offer.setUserId(userId);
        offer.setStatus(status);
        offer.update();
    }

    /**
     * 用户关系查询
     *
     * @return java.util.List<com.sw.fyzb.web.model.User>
     * @Param [userId, relationship]
     **/
    @Override
    public List<User> userRelationship(String userId, String relationship) {
        StringBuilder str = new StringBuilder();
        str.append(" select * from t_user t1 left join t_user_rel t2 on t1.user_id=t2.rel_user_id ")
                .append(" where  t2.user_id=? and t2.rel_type=? ");
        return User.dao.find(str.toString(), userId, relationship);
    }

    /**
     * 团队用户
     *
     * @return java.util.List<com.sw.fyzb.web.model.User>
     * @Date 2020/12/3 11:51
     * @Param [teamId]
     **/
    @Override
    public List<User> userTeam(String teamId) {
        StringBuilder str = new StringBuilder();
        str.append(" select * from t_user t1 left join t_team_member t2 on t1.user_id=t2.user_id where t2.team_id=? ");
        return User.dao.find(str.toString(), teamId);
    }
}
